package com.xiaodaidai.bllogsystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vdurmont.emoji.EmojiParser;
import com.xiaodaidai.bllogsystem.mapper.ArticleMapper;
import com.xiaodaidai.bllogsystem.mapper.CommentMapper;
import com.xiaodaidai.bllogsystem.mapper.StatisticMapper;
import com.xiaodaidai.bllogsystem.po.Article;
import com.xiaodaidai.bllogsystem.po.Statistic;
import com.xiaodaidai.bllogsystem.service.IArticleService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * IArticleService接口实现类
 * @Date 2021-3-14 13:14
 * @Created by XiaoDaiDai
 */
@Service
@Transactional
public class ArticleServiceImpl implements IArticleService {
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private StatisticMapper statisticMapper;
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private RedisTemplate redisTemplate;
    // 分页查询文章列表
    public PageInfo<Article> selectArticleWithPage(Integer page, Integer count) {
        PageHelper.startPage(page,count);
        List<Article> articleList = articleMapper.selectArticleWithPage();
        // 封装文章统计数据
        for(int i = 0; i < articleList.size();i++){
            Article article = articleList.get(i);
            Statistic statistic = statisticMapper.selectStatisticWithArticleId(article.getId());
            article.setHits(statistic.getHits());
            article.setCommentsNum(statistic.getCommentsNum());
        }
        PageInfo<Article> pageInfo = new PageInfo<>(articleList);
        return pageInfo;
    }

    // 统计前10的热度文章信息
    public List<Article> getHeatArticles() {
        List<Statistic> statisticList = statisticMapper.getStatistic();
        List<Article> articleList = new ArrayList<>();
        for(int i = 0; i < statisticList.size();i++){
            Article article = articleMapper.selectArticleWithId(statisticList.get(i).getArticleId());
            article.setHits(statisticList.get(i).getHits());
            article.setCommentsNum(statisticList.get(i).getCommentsNum());
            articleList.add(article);
            if(i>=9)
                break;
        }
        return articleList;
    }

    // 根据文章id查询单个文章详情
    public Article selectArticleWithId(Integer id) {
        Article article = null;
        //从缓存中查找
        Object articleTemp = redisTemplate.opsForValue().get("article_"+id);
        //如果缓存中存在，直接从缓存取
        //缓存中不存在，就从数据库中查找并存入缓存
        if(articleTemp!=null){
            article = (Article)articleTemp;
        }else {
            article = articleMapper.selectArticleWithId(id);
            if(article!=null){
                redisTemplate.opsForValue().set("article_"+id,article);
            }
        }
        return article;
    }

    // 发布文章
    public void publish(Article article) {
        //去除表情
        article.setContent(EmojiParser.parseToAliases(article.getContent()));
        //文章发布时间
        article.setCreated(new Date());
        //点击量
        article.setHits(0);
        //评论总量
        article.setCommentsNum(0);
        //发布文章
        articleMapper.publishArticle(article);
        //新增文章对应的统计信息
        statisticMapper.addStatistic(article);
    }

    // 根据主键更新文章
    public void updateArticleWithId(Article article) {
        //文章发布时间
        article.setModified(new Date());
        articleMapper.updateArticleWithId(article);
        //删除之前的缓存
        redisTemplate.delete("article_" +article.getId());
    }

    // 根据主键删除文章
    public void deleteArticleWithId(int id) {
        //删除文章
        articleMapper.deleteArticleWithId(id);
        //删除对应缓存
        redisTemplate.delete("article_"+id);
        //删除对应的文章统计数据
        statisticMapper.deleteStatisticWithId(id);
        //删除对应的文章评论数据
        commentMapper.deleteCommentWithId(id);
    }
}
